package cn.iocoder.yudao.module.system.controller.admin.mini.group.modul.controller.admin.group;

import cn.iocoder.yudao.module.system.controller.admin.mini.group.modul.controller.admin.group.vo.*;
import cn.iocoder.yudao.module.system.controller.admin.mini.group.modul.convert.group.GroupConvert;
import cn.iocoder.yudao.module.system.controller.admin.mini.group.modul.dal.dataobject.group.GroupDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.group.modul.service.group.GroupService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 拼团模块")
@RestController
@RequestMapping("/qd/group")
@Validated
public class GroupController {

    @Resource
    private GroupService groupService;

    @PostMapping("/create")
    @Operation(summary = "创建拼团模块")
    @PreAuthorize("@ss.hasPermission('qd:group:create')")
    public CommonResult<Integer> createGroup(@Valid @RequestBody GroupDO createReqVO) {
        return success(groupService.createGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新拼团模块")
    @PreAuthorize("@ss.hasPermission('qd:group:update')")
    public CommonResult<Boolean> updateGroup(@Valid @RequestBody GroupDO updateReqVO) {
        groupService.updateGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除拼团模块")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qd:group:delete')")
    public CommonResult<Boolean> deleteGroup(@RequestParam("id") Integer id) {
        groupService.deleteGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得拼团模块")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qd:group:query')")
    public CommonResult<GroupRespVO> getGroup(@RequestParam("id") Integer id) {
        GroupDO group = groupService.getGroup(id);
        return success(GroupConvert.INSTANCE.convert(group));
    }

    @GetMapping("/list")
    @Operation(summary = "获得拼团模块列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qd:group:query')")
    public CommonResult<List<GroupRespVO>> getGroupList(@RequestParam("ids") Collection<Integer> ids) {
        List<GroupDO> list = groupService.getGroupList(ids);
        return success(GroupConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得拼团模块分页")
    @PreAuthorize("@ss.hasPermission('qd:group:query')")
    public CommonResult getGroupPage(@Valid GroupPageReqVO pageVO) {
        return success(groupService.getGroupPage(pageVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出拼团模块 Excel")
    @PreAuthorize("@ss.hasPermission('qd:group:export')")
    @OperateLog(type = EXPORT)
    public void exportGroupExcel(@Valid GroupExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<GroupDO> list = groupService.getGroupList(exportReqVO);
        // 导出 Excel
        List<GroupExcelVO> datas = GroupConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "拼团模块.xls", "数据", GroupExcelVO.class, datas);
    }

}
